//
//  NJProductCell.m
//  09-彩票(lottery)
//
//  Created by apple on 14-6-17.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "NJProductCell.h"
#import "NJSettingItem.h"
#import "NJSettingArrowItem.h"
#import "NJSettingSwitchItem.h"
#import "NJSettingTextFieldItem.h"
#import "NJSettingButtonItem.h"
#import "TTSettingLabelItem.h"

@interface NJProductCell ()

@property (nonatomic, strong) UIImageView *arrowIv;
@property (nonatomic, strong) UISwitch *switchBtn;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *detailBtn;
@property (nonatomic, strong) UILabel *detailLabel;

@end

@implementation NJProductCell

- (UIImageView *)arrowIv {
    if (_arrowIv == nil) {
       _arrowIv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellArrow"]];
    }
    return _arrowIv;
}

- (UISwitch *)switchBtn {
    if (_switchBtn == nil) {
        _switchBtn = [[UISwitch alloc] init];
    }
    return _switchBtn;
}

- (UITextField *)textField {
    if (_textField == nil) {
        _textField = [[UITextField alloc] init];
        _textField.clearsOnBeginEditing = YES;
        _textField.bounds = CGRectMake(0, 0, 200, 44);
        _textField.textAlignment = NSTextAlignmentRight;
    }
    return _textField;
}

- (UIButton *)detailBtn {
    if (_detailBtn == nil) {
        _detailBtn = [[UIButton alloc] init];
        _detailBtn.frame = CGRectMake(0, 0, 80, 80);
        [_detailBtn addTarget:self action:@selector(detailBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _detailBtn;
}

- (UILabel *)detailLabel {
    if (_detailLabel == nil) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = [UIFont systemFontOfSize:14];
        _detailLabel.textColor = [UIColor blackColor];
        _detailLabel.numberOfLines = 1;
        _detailLabel.textAlignment = NSTextAlignmentRight;
        _detailLabel.frame = CGRectMake(0, 0, 150, 21);
    }
    return _detailLabel;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifier = @"cell";
    NJProductCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[NJProductCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)setItem:(NJSettingItem *)item {
    _item = item;
    // 设置数据
    self.textLabel.text = _item.tilte;
    if (item.icon) {
        self.imageView.image = [UIImage imageNamed:_item.icon];
    }
    
    // 设置辅助视图
    if ([_item isKindOfClass:[NJSettingArrowItem class]]) {
        self.accessoryView = self.arrowIv;
        self.selectionStyle = UITableViewCellSelectionStyleGray;
    }else if ([_item isKindOfClass:[NJSettingSwitchItem class]]) {
        self.accessoryView = self.switchBtn;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }else if([_item isKindOfClass:[NJSettingTextFieldItem class]]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        NJSettingTextFieldItem *item = (NJSettingTextFieldItem *)_item;
        self.textField.placeholder = item.placeholder;
        self.textField.keyboardType = item.keyboardType;
        self.accessoryView = self.textField;
    }else if ([_item isKindOfClass:[NJSettingButtonItem class]]){
        NJSettingButtonItem *item = (NJSettingButtonItem *)_item;
        [self.detailBtn setImage:[UIImage imageNamed:item.iconName] forState:UIControlStateNormal];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryView = self.detailBtn;
    }else if ([_item isKindOfClass:[TTSettingLabelItem class]]){
        TTSettingLabelItem *item = (TTSettingLabelItem *)_item;
        self.detailLabel.text = item.detailText;
        if (item.textColor) {
            self.detailLabel.textColor = item.textColor;
        }
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryView = self.detailLabel;
    }else {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryView = nil;
    }
    
}

#pragma makr - UIButton Action
- (void)detailBtnDidClick:(UIButton *)btn {
    if (self.item.option) {
        self.item.option();
    }
}
@end
